﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class mwc_admin_controls_housecategory : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string conn = ConfigurationSettings.AppSettings["mwc_con"];
        loadCatPanel();
    }
    protected void loadCatPanel() {
        string task = Request.QueryString["task"];
        switch (task)
        { 
            case "add":
                cat_input.Visible = true;
                cat_list.Visible = false;
                break;
            case "edit":
                editCat();
                cat_input.Visible = true;
                cat_list.Visible = false;
                break;
            case "view":
                
            default:
                loadListCat();
                cat_input.Visible = false;
                cat_list.Visible = true;
                break;
        }
    }
    protected void loadListCat() { 
        string msg = Request.QueryString["msg"];
        if (msg != "")
        {
            list_tb.Text = msg;
        }
        string query = "SELECT * FROM mwc_housecategory";
        try
        {
            SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
            sc.Open();
            SqlDataAdapter sda = new SqlDataAdapter(query, sc);
            DataSet ds = new DataSet("housecategory");
            sda.Fill(ds);
            gvListCat.DataSource = ds;
            gvListCat.DataBind();
        }
        finally { 
        }
       
    }
    protected void saveCat(object sender, EventArgs e) {
        string task = Request.QueryString["task"];
        switch (task) { 
            case "add":
                addCat();
                break;
            case "edit":
                updateCat();
                break;
            default:
                Response.Redirect("~/mwc_admin/Panel.aspx?option=housecategory");
                break;
        }
    }
    protected void addCat() { 
        string _cat_name = cat_name.Text;
        string _cat_enable = cat_enable.Checked.ToString();
        string _cat_enable2 = _cat_enable == "true" ? "1" : "0";
        string query = "INSERT INTO mwc_housecategory(catname,enable) VALUES('" + _cat_name + "', " + _cat_enable2 + ")";
        SqlConnection scn = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        try
        {
            scn.Open();
            SqlCommand scmd = new SqlCommand(query, scn);
            scmd.ExecuteNonQuery();
        }
        catch {
            Response.Redirect("~/mwc_admin/Default.aspx");
        }
        finally {
            scn.Close();
            Response.Redirect("~/mwc_admin/Panel.aspx?option=housecategory&msg=Save successfully");
        }

    }
    protected void editCat() {
        string _id = Request.QueryString["id"];
        //cat_name.Text = _id;
        try
        {
            int temp = Convert.ToInt32(_id);
            loadCategoryWithID(_id);
        }
        catch {
            Response.Redirect("~/mwc_admin/Panel.aspx?option=housecategory&msg=This category is not exist!");
        }
    }

    protected void updateCat() {
        //cat_name.Text = "aaaaaaaaaaaaaa";
        string _enable = (cat_enable.Checked == true) ? "1" : "0";
        string query = "UPDATE mwc_housecategory SET catname = '" + cat_name.Text + "', enable = " + _enable + " WHERE id = " + Request.QueryString["id"];
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        sc.Open();
        SqlCommand scmd = new SqlCommand(query, sc);
        try{
            scmd.ExecuteNonQuery();
            Response.Redirect("~/mwc_admin/Panel.aspx?option=housecategory");
        }
        catch (Exception e) { 
            
        }
        sc.Close();
    }

    protected void loadCategoryWithID(string _id) {
        string query = "SELECT * FROM mwc_housecategory WHERE id = " + _id;
        //string my = ConfigurationSettings.AppSettings["mwc_con"];
        SqlConnection sc = new SqlConnection(ConfigurationSettings.AppSettings["mwc_con"]);
        try{
            sc.Open();
            SqlCommand scmd = new SqlCommand(query, sc);
            SqlDataReader sdr = scmd.ExecuteReader();
            while (sdr.Read())
            {
                cat_name.Text = sdr["catname"].ToString();
                //string temp = sdr["enable"].ToString();
                cat_enable.Checked = (bool)sdr["enable"];
                //detail_tb.Text = temp;
                //if (temp == "true") cat_enable.Checked = true;
                //else cat_enable.Checked = false;
            }
            sdr.Close();
        }finally {
            sc.Close();
        }
    }
    protected void turnBack(object sender, EventArgs e)
    {
        Response.Redirect("~/mwc_admin/Panel.aspx?option=housecategory");
    }
}
